module ApplicationHelper
  
  #被访问: :browser_version
  def accessed_by?(args = {})
    request.env['HTTP_USER_AGENT'].downcase.index(args[:browser_version])
  end
  
  # qtip args :content :tip :target :tooltip
  def qtip(jquery_selector,args = {})
    raw "$('#{jquery_selector}').qtip({
     content: '#{args[:content]}',
     style: {
       name : 'cream',
       tip: '#{args[:tip]}',
       border: {
         radius: 5
       }
     },           
     position: {
       corner: {
         target: '#{args[:target]}',
         tooltip: '#{args[:tooltip]}'
       }
     },
     show: {
       ready : true
     }
  })"
  end

  def loading_image_tag( size = :normal, options = {})
    options[:title] ||= '读取中...'
    options[:alt] ||= options[:title]
    case size
    when :large
      options[:class] ||= 'loading large'
      image_tag 'loading/54x18.gif', options
    when :normal
      options[:class] ||= 'loading normal'
      image_tag 'loading/36x12.gif', options
    when :small
      options[:class] ||= 'loading small'
      image_tag 'loading/24x8.gif', options
    else
      raise "未知尺码：#{size}"
    end
  end

  def current_controller?(name_or_names)
    name_or_names.is_a?(Array) ? name_or_names.include?(params[:controller]) : params[:controller] == name_or_names
  end

  def number_to_rmb(rmb)
    number_to_currency rmb, :format => "%n", :precision => 0
  end

  def label_js_for(target)
    raw "
      <script type='text/javascript' charset='utf-8'>
        $('#label_for_#{target}').click(function() {
          $(this).hide();
          $('##{target}').show();
        });
        $('##{target}').blur(function() {
          if($(this).val() == '' ) {
            $(this).hide();
            $('#label_for_#{target}').show();
          }
        });
        if($('##{target}').val() != ''){
          $('##{target}').show();
          $('#label_for_#{target}').hide();
        } else {
          $('##{target}').hide();
          $('#label_for_#{target}').show();
        }
      </script>
    "
  end
end